package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tresidence;
import com.xy.manage.service.TresidenceService;



@Controller
public class TresidenceAction{
	private static final Logger log = LoggerFactory.getLogger(TresidenceAction.class);
	public TresidenceAction() {
	}
 
 
 	/**
	 * 居住证网上办理新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tresidenceAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tresidence", new Tresidence());
        setDict(model);
		return "tresidenceEdit";
	}

	/**
	 * 居住证网上办理新增保存
	 * 
	 * @param tresidence
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tresidenceSave.do")
	public String save(Tresidence tresidence, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tresidence, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tresidence = tresidenceService.save(tresidence);
		return "redirect:v_tresidenceList.do";
	}

	/**
	 * 居住证网上办理删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tresidenceDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tresidenceService.remove(id);
			}
		}
		return "redirect:v_tresidenceList.do";
	}

	/**
	 * 居住证网上办理编辑页面
	 * 
	 * @param residenceid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tresidenceEdit.do")
	public String edit(long residenceid, HttpServletRequest request, ModelMap model) {
		Tresidence tresidence = tresidenceService.get(residenceid);
		model.addAttribute("tresidence", tresidence);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tresidenceEdit";
	}

	/**
	 * 居住证网上办理详情
	 * 
	 * @param residenceid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tresidenceDetail.do")
	public String detail(long residenceid, HttpServletRequest request, ModelMap model) {
		Tresidence tresidence = tresidenceService.get(residenceid);
		model.addAttribute("tresidence", tresidence);
        setDict(model);
		return "tresidenceView";
	}

	/**
	 * 居住证网上办理查询（分页）
	 * 
	 * @param tresidence
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tresidenceList.do")
	public String listPage(Tresidence tresidence, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tresidenceService.getTresidenceByPage(toMap(tresidence),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tresidenceList";
	}

	/**
	 * 居住证网上办理查询
	 * 
	 * @param tresidence
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tresidenceList1.do")
	public String list(Tresidence tresidence, HttpServletRequest request, ModelMap model) {
		List<Tresidence> list = tresidenceService.getTresidence(toMap(tresidence));
		model.addAttribute("list", list);
        setDict(model);
		return "tresidenceList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tresidence
	 * @return
	 */	private Map<String,Object> toMap(Tresidence tresidence){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tresidence.getResidenceid()!=null&&!tresidence.getResidenceid().equals("")){
			map.put("Residenceid", tresidence.getResidenceid());
		}
		if(tresidence.getName()!=null&&!tresidence.getName().equals("")){
			map.put("Name", tresidence.getName());
		}
		if(tresidence.getSFZH()!=null&&!tresidence.getSFZH().equals("")){
			map.put("SFZH", tresidence.getSFZH());
		}
		if(tresidence.getSJH()!=null&&!tresidence.getSJH().equals("")){
			map.put("SJH", tresidence.getSJH());
		}
		if(tresidence.getSZXPCJBM()!=null&&!tresidence.getSZXPCJBM().equals("")){
			map.put("SZXPCJBM", tresidence.getSZXPCJBM());
		}
		if(tresidence.getExpressage()!=null&&!tresidence.getExpressage().equals("")){
			map.put("Expressage", tresidence.getExpressage());
		}
		if(tresidence.getBid()!=null&&!tresidence.getBid().equals("")){
			map.put("Bid", tresidence.getBid());
		}
		if(tresidence.getStatus()!=null&&!tresidence.getStatus().equals("")){
			map.put("Status", tresidence.getStatus());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("expressageMap", DictUtils.getDictByType(DictUtils.TRESIDENCE_EXPRESSAGE));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tresidence tresidence, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TresidenceService tresidenceService;   
}